package com.qianclass.talklive.app.activity;

import com.qianclass.talklive.app.utils.LogUtil;

import io.agora.rtc.IRtcEngineEventHandler;

/**
 * Created by apple on 15/9/16.
 */
public class MessageHandler extends IRtcEngineEventHandler {

	private BaseEngineEventHandlerActivity mHandlerActivity;
	//用户成功加入频道的回调
	public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
		 BaseEngineEventHandlerActivity activity = getActivity();

	        if (activity != null) {
	            activity.onJoinChannelSuccess(channel, uid,elapsed);
	        }
    }
	
	
	
    //显示房间内其他用户的视频
    @Override
    public void onFirstRemoteVideoDecoded(int uid, int width, int height, int elapsed) {

        BaseEngineEventHandlerActivity activity = getActivity();

        if (activity != null) {
            activity.onFirstRemoteVideoDecoded(uid, width, height, elapsed);
        }
    }

    //远端视频显示回调
    @Override
    public void onFirstRemoteVideoFrame(int uid, int width, int height, int elapsed) {
        BaseEngineEventHandlerActivity activity = getActivity();

        if (activity != null) {
            activity.onFirstRemoteVideoFrame(uid, width, height, elapsed);
        }
    }

    //本地视频显示回调
    @Override
    public void onFirstLocalVideoFrame(int width, int height, int elapsed) {
        BaseEngineEventHandlerActivity activity = getActivity();

        if (activity != null) {
            activity.onFirstLocalVideoFrame(width, height, elapsed);
        }

    }

    @Override
    public void onCameraReady() {
        BaseEngineEventHandlerActivity activity = getActivity();

        if (activity != null) {
            activity.onCameraReady();
        }
    }

    //用户进入
    @Override
    public void onUserJoined(int uid, int elapsed){
        LogUtil.d("Test", "IRtcEngineEventHandler _ onUserJoined用户进入");
        BaseEngineEventHandlerActivity activity = getActivity();

        if (activity != null) {
            activity.onUserJoined(uid, elapsed);
        }
    }

    //用户退出
    @Override
    public void onUserOffline(int uid, int reason) {

        BaseEngineEventHandlerActivity activity = getActivity();
        LogUtil.d("Test", "IRtcEngineEventHandler _ onUserOffline用户退出");
        if (activity != null) {
            activity.onUserOffline(uid);
        }
    }

    @Override
    public void onUserEnableVideo(int uid, boolean enabled) {
        BaseEngineEventHandlerActivity activity = getActivity();

        if (activity != null) {
            activity.onUserEnableVideo(uid, enabled);
        }
    }

    //监听其他用户是否关闭视频
    @Override
    public void onUserMuteVideo(int uid,boolean muted){

        BaseEngineEventHandlerActivity activity = getActivity();

        if (activity != null) {
            activity.onUserMuteVideo(uid, muted);
        }
    }
    //监听其他用户是否关闭音频
    @Override
    public void onUserMuteAudio(int uid,boolean muted){
    	
    	BaseEngineEventHandlerActivity activity = getActivity();
    	
    	if (activity != null) {
    		activity.onUserMuteAudio(uid, muted);
    	}
    }

    //更新聊天数据
    @Override
    public void onRtcStats(RtcStats stats){

        BaseEngineEventHandlerActivity activity = getActivity();

        if (activity != null) {
            activity.onUpdateSessionStats(stats);
        }
    }


    @Override
    public void onLeaveChannel(RtcStats stats) {
        BaseEngineEventHandlerActivity activity = getActivity();

        if (activity != null) {
            activity.onLeaveChannel(stats);
        }
    }


    @Override
    public void onError(int err) {
        BaseEngineEventHandlerActivity activity = getActivity();

        if (activity != null) {
            activity.onError(err);
        }
    }

    public void setActivity(BaseEngineEventHandlerActivity activity) {

        this.mHandlerActivity = activity;
    }

    public BaseEngineEventHandlerActivity getActivity(){

        return mHandlerActivity;
    }
}
